package com.weichuang.ecommerce;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;

/** 
 * <p>Copyright: All Rights Reserved</p>  
 * <p>Company: 指点无限(北京)科技有限公司   http://www.weichuangwuxian.cn</p>
 * <p>Description: 输出工具提供者 </p>
 * <p>Author:jmzhang/张际明, 16/09/29</p>
 */
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class GsonProvider extends JacksonJsonProvider {
	private static final Log log = LogFactory.getLog(GsonProvider.class);
	
	public GsonProvider() {
		log.debug("Initialization Gson Provider...");
	}
	
	@Override
	public long getSize(Object obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
		return -1;
	}
	
	@Override
	public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
		return true;
	}
	
	@Override
	public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
			MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
		ObjectMapper mapper = locateMapper(type, mediaType);
		mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		JsonParser jp = mapper.getJsonFactory().createJsonParser(entityStream);
		jp.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
		return mapper.readValue(jp, mapper.constructType(genericType));
	}
}
